//
//  QueryHistoryViewController.m
//  CityFire
//
//  Created by  androidlongs on 2017/11/1.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "QueryHistoryViewController.h"
#import "MonitorHistoryTableViewCell.h"
#import "HistoryDetaileViewController.h"

@interface QueryHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *selectQueryTypeSegment;

@property (nonatomic, strong) NSMutableArray *newDeviceArray;
@property (nonatomic, strong) NSMutableArray *devicesArray;

@end

@implementation QueryHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"历史记录";
    
    [self createUiView];
    
    //[self loadingDatas];
}

- (void)viewWillAppear:(BOOL)animated {
     [self loadingDatas];
    [self.newDeviceArray removeAllObjects];
    [self.tableView reloadData];
}
-(void) createUiView{
    self.refreshScrollView = self.tableView;
    self.isAutoRefresh = YES;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    
    [self.selectQueryTypeSegment addTarget:self action:@selector(didClicksegmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    
   
    
    @weakify(self);
    self.refreshActionBlock = ^(){
        @strongify(self);
        [self loadingDatas];
        
    };
    
    self.loadMoreActionBlock = ^(){
        @strongify(self);
        [self loadingDatas];
        
        //        [self getMessageList];
    };
    [self.tableView reloadData];
    self.tableView.tableFooterView = [UIView new];
    
    [RACObserve(self.monitorViewModel, requestError) subscribeNext:^(NSError *requestError) {
        [self endRefresh];
    }];
    
    [RACObserve(self.monitorViewModel, responseInfo) subscribeNext:^(NSError *requestError) {
        [self endRefresh];
    }];
    
    [RACObserve(self.monitorViewModel, monitorArray) subscribeNext:^(NSArray *mArray) {
        [self endRefresh];
        
        if (mArray.count) {
            
            self.devicesArray = [mArray copy];
            [self.newDeviceArray removeAllObjects];
            for (MonitorModel *model in mArray) {
                switch (self.selectedHistoryIndex) {
                    case 0:
                        if ([model.status1 isEqualToString:@"02"]) {
                            [self.newDeviceArray addObject:model];
                        }
                        break;
                    case 1:
                        if ([model.status1 isEqualToString:@"03"]) {
                            [self.newDeviceArray addObject:model];
                        }
                        
                        break;
                        
                    default:
                        break;
                }
                
            }
            [self.tableView reloadData];
        }else if (mArray) {
            [SVProgressHUD showInfoWithStatus:@"没有数据～～"];
            [self.tableView reloadData];
        }
    }];
    
    [RACObserve(self.monitorViewModel, countModel) subscribeNext:^(MonitorCountModel *countModel) {
        [self endRefresh];
        
        if ([countModel.waringSize integerValue]) {
            self.navigationController.tabBarItem.badgeValue = [countModel.waringSize stringValue];
        }else {
            self.navigationController.tabBarItem.badgeValue = nil;
        }
    }];
}
-(void) loadingDatas{
    [self.monitorViewModel  getQueryHistoryWarning:@{@"start_date" : self.startTime, @"end_date" : self.endTime, @"type" : self.selectedHistoryIndex == 0 ? @"报警" : @"故障",@"keywords":self.searchContent}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (MonitorViewModel *)monitorViewModel
{
    if (_monitorViewModel == nil) {
        _monitorViewModel = [[MonitorViewModel alloc] init];
    }
    
    return _monitorViewModel;
}


- (NSMutableArray *)newDeviceArray
{
    if (_newDeviceArray == nil) {
        _newDeviceArray = [NSMutableArray array];
    }
    
    return _newDeviceArray;
}

- (NSMutableArray *)devicesArray
{
    if (_devicesArray == nil) {
        _devicesArray = [NSMutableArray array];
    }
    
    return _devicesArray;
}

-(void)didClicksegmentedControlAction:(UISegmentedControl *)Seg{
    
    NSInteger index = Seg.selectedSegmentIndex;
    self.selectedHistoryIndex=index;
    [self loadingDatas];

}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 172;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.monitorViewModel.monitorArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MonitorModel *model =  self.monitorViewModel.monitorArray[indexPath.row];
    
    MonitorHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MonitorHistoryTableViewCell"];
    
    if (cell == nil) {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"MonitorHistoryTableViewCell" owner:nil options:nil] firstObject];
        //        cell = [[MonitorHistoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MonitorHistoryTableViewCell"];
    }
    [cell setMonitorModel:model];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MonitorModel *model =  self.monitorViewModel.monitorArray[indexPath.row];
    HistoryDetaileViewController *contryll=[[HistoryDetaileViewController alloc]init];
    contryll.monitorModel = model;
    [self.navigationController pushViewController:contryll animated:YES];
    
}

@end
